package com.kerrykidz.scmstock.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmOutStgEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmOutstgPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmPrdStgEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmOutStgEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmOutstgPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPrdStgEntity;
import com.kerrykidz.scmstock.service.IStockGoodsOutService;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockGoodsOutServiceImpl
 * @description: 商品出库服务类
 * @author: huanjiang
 * @date: 2014年8月21日 下午4:39:34
 * @version: V1.0
 * 
 */
@Service
public class StockGoodsOutServiceImpl implements IStockGoodsOutService {

	@Autowired
	// 出库记录表
	private ScmOutStgEntityMapper scmOutStgEntityMapper;

	@Autowired
	// 商品库存表
	private ScmPrdStgEntityMapper scmPrdStgEntityMapper;

	@Autowired
	// 出库商品明细表
	private ScmOutstgPrdEntityMapper scmOutstgPrdEntityMapper;

	@Autowired
	// 商品信息
	private GblScmPrdEntityMapper gblScmPrdEntityMapper;

	/**
	 * 
	 * @Title: findscmOutStg
	 * @Description: 出库记录表查询,用于分页
	 * @param: ScmOutStgEntity
	 * @date: 2014年8月20日
	 * @return: ScmOutStgEntity
	 * @throws RDPException
	 * 
	 */
	public ScmOutStgEntity findscmOutStg(ScmOutStgEntity scmOutStg)
			throws RDPException {
		scmOutStg.setIspaging(true);
		List<ScmOutStgEntity> beanList = scmOutStgEntityMapper
				.findScmOutStg(scmOutStg);
		if (beanList.size() > 0) {
			scmOutStg.setResults(beanList);
		}
		return scmOutStg;
	}

	/**
	 * 
	 * @Title: updatefindscmOutStgForDel
	 * @Description: 更新出库单删除标记
	 * @param: pkOutstgId 出库单编号
	 * @date: 2014年8月21日
	 * @return: void
	 * @throws RDPException
	 * 
	 */
	public void updatefindscmOutStgForDel(String pkOutstgId)
			throws RDPException {
		scmOutStgEntityMapper.updatefindScmOutStgForDel(pkOutstgId);
	}

	/**
	 * 
	 * @Title: rollBackForStockOut
	 * @Description: 更新出库记录表后回滚库存数据
	 * @param: pkOutstgId 出库单编号
	 * @param: ScmPrdStgEntity
	 * @date: 2014年8月21日
	 * @return: void
	 * @throws RDPException
	 * 
	 */
	public synchronized boolean rollBackForStockOut(String pkOutstgId,
			ScmPrdStgEntity scmPrdStg) throws RDPException {
		try {
			ScmOutStgEntity scmOutStg = new ScmOutStgEntity();
			scmOutStg.setPkOutstgId(pkOutstgId);
			List<ScmOutStgEntity> beanList = scmOutStgEntityMapper
					.findScmOutStg(scmOutStg);
			if (beanList.size() == 0) {
				return false;
			}
			ScmOutStgEntity scmOutStgBean = beanList.get(0);
			String brandId = scmOutStgBean.getFkBranId();
			List<ScmOutstgPrdEntity> ScmOutstgPrdList = scmOutstgPrdEntityMapper
					.findScmOutstgPrd(pkOutstgId);
			if (ScmOutstgPrdList.size() > 0) {
				for (ScmOutstgPrdEntity sOPrd : ScmOutstgPrdList) {
					ScmPrdStgEntity updateScmPrdStg = new ScmPrdStgEntity();
					updateScmPrdStg.setFkPrdId(sOPrd.getFkPrdId());
					updateScmPrdStg.setInstgCnt(sOPrd.getOutstgCnt());
					updateScmPrdStg.setFkBranId(brandId);
					updateScmPrdStg.setModiBy(scmOutStgBean.getModiBy());
					scmPrdStgEntityMapper.updateScmPrdStg(updateScmPrdStg);
					//修改商品表Mode_time
					GblScmPrdEntity prod = new GblScmPrdEntity();
					prod.setPkPrdId(scmPrdStg.getFkPrdId());
					prod.setModiTime(scmPrdStg.getModiTime());
					gblScmPrdEntityMapper.updateEntity(prod);
				}
			}
			updatefindscmOutStgForDel(pkOutstgId);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @Title: stockGoodsOut
	 * @Description: 出库
	 * @param: scmOutStg
	 * @param: contents 包含出库商品数据字符串
	 * @date: 2014年8月27日
	 * @return: void
	 * @throws RDPException
	 * 
	 */
	public synchronized void doStockGoodsOut(ScmOutStgEntity scmOutStg,
			String contents) throws RDPException {
		// 门店编号
		String brandId = scmOutStg.getFkBranId();
		String modiBy = scmOutStg.getModiBy();
		scmOutStgEntityMapper.insertScmOutStg(scmOutStg);
		String[] content = contents.split("[|]");
		for (String prds : content) {
			// data[0]商品id data[1]出货数量 data[2]签收人 data[3]备注data[4]库存数量
			String[] data = prds.split("_");
			ScmOutstgPrdEntity scmOutstgPrd = new ScmOutstgPrdEntity();
			scmOutstgPrd.setFkOutstgId(scmOutStg.getPkOutstgId());
			scmOutstgPrd.setFkPrdId(data[0]);
			// 出货数量
			int outstgCnt = Integer.parseInt(data[1]);
			scmOutstgPrd.setOutstgCnt(Integer.parseInt(data[1]));
			scmOutstgPrd.setSignature(data[2]);
			if (data.length > 3) {
				scmOutstgPrd.setComments(data[3]);
			}
			//库存数量  用于历史查询
			scmOutstgPrd.setStgCnt(Integer.parseInt(data[4]));
			scmOutstgPrdEntityMapper.insertScmOutstgPrd(scmOutstgPrd);// 一类商品对应一条数据
			ScmPrdStgEntity scmPrdStg = new ScmPrdStgEntity();
			scmPrdStg.setInstgCnt(0 - outstgCnt);
			scmPrdStg.setFkBranId(brandId);
			scmPrdStg.setFkPrdId(data[0]);
			scmPrdStg.setModiBy(modiBy);
			scmPrdStgEntityMapper.updateScmPrdStg(scmPrdStg);// 减少商品库存
			
			//修改商品表Mode_time
			GblScmPrdEntity prod = new GblScmPrdEntity();
			prod.setPkPrdId(scmPrdStg.getFkPrdId());
			prod.setModiTime(scmPrdStg.getModiTime());
			gblScmPrdEntityMapper.updateEntity(prod);
		}
	}

	/**
	 * 
	 * @Title: findScmOutStgDetail
	 * @Description: 出库清单，根据出库单获得对应的出库单信息，包括门店名称
	 * @param pkOutstgId
	 *            出库单号
	 * @throws RDPException
	 * @date: 2014年8月29日
	 * @return:ScmOutStgEntity
	 */
	public ScmOutStgEntity findScmOutStgDetail(String pkOutstgId)
			throws RDPException {

		return scmOutStgEntityMapper.findScmOutStgDetail(pkOutstgId);
	}

	/**
	 * 
	 * @Title: findProductListForStock
	 * @Description: 通过商品id和门店id，获得对应商品信息
	 * @param entity
	 * @throws RDPException
	 * @date: 2014年8月29日
	 * @return:List<GblScmPrdEntity>
	 */
	public List<GblScmPrdEntity> findProductListForStock(GblScmPrdEntity entity)
			throws RDPException {
		return gblScmPrdEntityMapper.findProductListForStock(entity);
	}

	/**
	 * 
	 * @Title: getProductListForDetail
	 * @Description: 获得出库清单中商品列表信息
	 * @param fkOutstgId
	 *            出库单id
	 * @param fkBranId
	 *            门店id
	 * @throws RDPException
	 * @date: 2014年9月1日
	 * @return:List<GblScmPrdEntity>
	 */
	public List<ScmOutstgPrdEntity> getProductListForDetail(String fkOutstgId) throws RDPException {
		return scmOutstgPrdEntityMapper.findScmOutstgPrd(fkOutstgId);
		/*if (ScmOutstgPrdList.size() > 0) {
			List<GblScmPrdEntity> beanlist = new ArrayList<GblScmPrdEntity>();
			GblScmPrdEntity entity = new GblScmPrdEntity();
			entity.setFkBranId(fkBranId);
			for (ScmOutstgPrdEntity sop : ScmOutstgPrdList) {
				entity.setPkPrdId(sop.getFkPrdId().trim());
				List<GblScmPrdEntity> GblScmPrdList = gblScmPrdEntityMapper
						.findProductListForStock(entity);
				if (GblScmPrdList.size() > 0) {
					for (GblScmPrdEntity gsp : GblScmPrdList) {
						// 返回的实例中加入出库信息
						gsp.setComments(sop.getComments());
						gsp.setOutstgCnt(sop.getOutstgCnt());
						gsp.setSignature(sop.getSignature());
						beanlist.add(gsp);
					}
				}
			}
			return beanlist;
		}
		return null;*/
	}

}
